package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.mapper.SysAppErrorLogMapper;
import com.settlement.system.model.entity.SysAppErrorLog;
import com.settlement.system.model.form.SysAppErrorLogForm;
import com.settlement.system.model.query.SysAppErrorLogQuery;
import com.settlement.system.model.vo.SysAppErrorLogImportVo;
import com.settlement.system.service.SysAppErrorLogService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * APP运行错误表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-03-08
 */
@Service
public class SysAppErrorLogServiceImpl extends ServiceImpl<SysAppErrorLogMapper, SysAppErrorLog> implements SysAppErrorLogService {


    @Value("${oss.minio.endpoint}")
    private String endpoint;

    @Value("${oss.minio.bucket-name}")
    private String bucketName;

    @Override
    public Page<SysAppErrorLog> getPage(SysAppErrorLogQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysAppErrorLog> page = new Page<>(pageNum, pageSize);
        Page<SysAppErrorLog> mapperPage = this.baseMapper.getPage(page, queryParams);
        for (SysAppErrorLog record : mapperPage.getRecords()) {
            record.setFilePath(endpoint + File.separator + bucketName + File.separator + record.getFilePath());
        }
        return mapperPage;
    }

    @Override
    public boolean saveSysAppErrorLog(SysAppErrorLogForm sysAppErrorLogForm) {
        SysAppErrorLog sysAppErrorLog = new SysAppErrorLog();
        BeanUtil.copyProperties(sysAppErrorLogForm, sysAppErrorLog);
        sysAppErrorLog.setCreateTime(null);
        return this.saveOrUpdate(sysAppErrorLog);
    }

    @Override
    public SysAppErrorLogForm getSysAppErrorLogFormData(Long id) {
        SysAppErrorLog sysAppErrorLog = this.getById(id);
        SysAppErrorLogForm sysAppErrorLogForm = new SysAppErrorLogForm();
        BeanUtil.copyProperties(sysAppErrorLog, sysAppErrorLogForm);
        return sysAppErrorLogForm;
    }

    @Override
    public boolean deleteSysAppErrorLog(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysAppErrorLogImportVo> voList) {
        List<SysAppErrorLog> sysAppErrorLogList = new ArrayList<>();
        for (SysAppErrorLogImportVo importVo : voList) {
            SysAppErrorLog sysAppErrorLog = new SysAppErrorLog();
            BeanUtil.copyProperties(importVo, sysAppErrorLog);
            sysAppErrorLogList.add(sysAppErrorLog);
        }
        this.saveOrUpdateBatch(sysAppErrorLogList);
    }

    @Override
    public List<SysAppErrorLogImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysAppErrorLog> sysAppErrorLogList = this.getBaseMapper().selectBatchIds(ids);
        List<SysAppErrorLogImportVo> voList = new ArrayList<>();
        for (SysAppErrorLog sysAppErrorLog : sysAppErrorLogList) {
            SysAppErrorLogImportVo vo = new SysAppErrorLogImportVo();
            BeanUtil.copyProperties(sysAppErrorLog, vo);
            voList.add(vo);
        }
        return voList;
    }
}
